package com.egg.auth.service.login.factory;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.egg.auth.entity.user.User;
import com.egg.auth.entity.user.UserAccount;
import com.egg.auth.enums.account.AccountStateEnum;
import com.egg.auth.enums.account.AccountTypeEnum;
import com.egg.auth.enums.account.DeviceTypeEnum;
import com.egg.auth.enums.user.UserStateEnum;
import com.egg.auth.model.LoginUserInfo;
import com.egg.auth.model.request.Login;
import com.egg.auth.service.login.ILoginFactory;
import com.egg.auth.service.login.LoginHelper;
import com.egg.auth.service.role.IRoleLinkUserService;
import com.egg.auth.service.user.IUserAccountService;
import com.egg.auth.service.user.IUserService;
import com.egg.auth.utils.WechatUtils;
import com.egg.common.core.enums.ResponseCode;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.IdGenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

/**
 * @author east
 */
@Slf4j
@Service
public class LoginFactoryWechat implements ILoginFactory, InitializingBean {


    @Autowired
    private IUserAccountService userAccountService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleLinkUserService roleLinkUserService;


    @Override
    public void afterPropertiesSet() {
        container.put(AccountTypeEnum.WECHAT.getCode(), this);
    }

    @Transactional(rollbackFor = Throwable.class)
    public UserAccount initUserAccount(String username) {
        User user = new User();
        user.setState(UserStateEnum.ENABLED.getCode());
        user.setNickname(IdGenUtil.strId().substring(6));
        userService.baseAdd(user);
        UserAccount userAccount = new UserAccount();
        userAccount.setAccountType(AccountTypeEnum.WECHAT.getCode());
        userAccount.setUsername(username);
        userAccount.setState(AccountStateEnum.ENABLED.getCode());
        userAccount.setUserId(user.getId());
        userAccountService.baseAdd(userAccount);
        roleLinkUserService.initOnApi(user.getId());
        return userAccount;
    }

    @Override
    public ResponseData<String> login(Login login) {
        if (ObjectUtil.isEmpty(login.getCode())) {
            return ResponseData.error(ResponseCode.CUSTOM_ERROR.getCode(), "code缺失");
        }
        String phone = WechatUtils.getPhone(login.getCode());
        Optional<UserAccount> opt = userAccountService.findByUsername(AccountTypeEnum.WECHAT.getCode(), phone);
        UserAccount userAccount = opt.orElseGet(() -> initUserAccount(phone));
        if (!Objects.equals(AccountStateEnum.ENABLED.getCode(), userAccount.getState())) {
            return ResponseData.error(ResponseCode.CUSTOM_ERROR.getCode(), "账号非启用状态");
        }
        LoginUserInfo loginUserInfo = LoginHelper.buildLoginUser(AccountTypeEnum.WECHAT, opt.get().getUserId());
        LoginHelper.login(loginUserInfo, DeviceTypeEnum.get(login.getDeviceType()));
        return ResponseData.success(StpUtil.getTokenValue());
    }


}
